// pages/ZBAuthorize/index.js
//获取应用实例
const common = require("../../public.js")
const app = getApp()

Page({
  data: {
    // phoneNumber:'',
    userInfoObj: {},
    // telephone: '',
    backUrl: '',
    loading: false,
    active: 0,
    steps: [
      { text: '步骤一', desc: '授权登录' },
      { text: '步骤二', desc: '绑定手机号' }
    ]
  },
  onLoad: function (options) {
    if (options.backUrl) {
      this.data.backUrl = decodeURIComponent(options.backUrl)
    } else {
      this.data.backUrl = '/pages/index/index'
    }
  },
  cancel() {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  bindGetUserProfile() {
    const that = this
    if (wx.getUserProfile) {
      wx.getUserProfile({
        desc: '用于完善用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        success: (res) => {
          that.getUserInfoSuccess(res)
        }
      })
    } else {
      // 兼容部分旧的版本
      wx.getUserInfo({
        success: (res) => {
          that.getUserInfoSuccess(res)
        }
      })
    }
  },
  getUserInfoSuccess(res) {
    this.data.userInfoObj = res.userInfo
    this.setData({
      active: 1,
    })
  },
  // bindGetUserInfo(e) {
  //   const that = this
  //   const tempJson = {
  //     openid: app.globalData.openid,
  //     encryptedData: e.detail.encryptedData,
  //     iv: e.detail.iv
  //   }
  //   common.post("/user/wxxcx/denWxxcxPhoneByOpenid", tempJson, function (res) {
  //     if (res.status === 200) {
  //       console.log('个人信息', JSON.parse(res.data))
  //       let userInfo = JSON.parse(res.data)
  //       app.globalData.unionId = userInfo.unionId
  //       app.globalData.openId = userInfo.openId
  //       that.setData({
  //         getInfoStatus: true,
  //         userInfoObj: userInfo
  //       })
  //     } else {
  //       common.showRequestErrMsg(res)
  //     }
  //   })
  // },

  getPhoneNumber: function (e) {
    const that = this
    const tempJson = {
      openid: app.globalData.openid,
      encryptedData: e.detail.encryptedData,
      iv: e.detail.iv
    } 
    that.setData({
      loading: true
    })
    common.post('/user/wxxcx/denWxxcxPhoneByOpenid', tempJson, function (res) {
      if (res.status === 200) {
        if (res.data) {
          const json = JSON.parse(res.data);
          that.bindWxinFunc(json.phoneNumber)
        } else {
          common.showToast('获取手机号失败，请稍后再试')
        }
      } else {
        common.showRequestErrMsg(res)
      }
    }, function () {
      that.setData({
        loading: false
      })
    })
  },
  bindWxinFunc(phoneParam) {
    const that = this
    that.setData({
      loading: true
    })

    const tempJson = {
      userName: that.data.userInfoObj.nickName,
      headImgUrl: that.data.userInfoObj.avatarUrl,
      telephone: phoneParam,
      sex: that.data.userInfoObj.gender,
      xcxOpenId: app.globalData.openid,
      unionId: app.globalData.unionId
    }
    common.post('/user/bindWxxcxInfo', tempJson, function (res) {
      if (res.status == 200) {
        wx.setStorage({
          key: "user",
          data: res.data
        })

        var dealbackUrl = that.data.backUrl.replace(/\"/g, "");
        wx.redirectTo({
          url: dealbackUrl
        })
      } else {
        common.showRequestErrMsg(res)
      }
    }, function () {
      that.setData({
        loading: false
      })
    })
  }
})